<?php
/**
 * Created by PhpStorm.
 * User: chenjunjie
 * Date: 2020/8/25
 * Time: 2:47 PM
 */
namespace App\Task;

use App\Model\XApiCompany;
use App\Model\XApiUser;
use App\Model\ZaXApiCompany;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\Di\Annotation\Inject;

use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;

/**
 * @Crontab(name="SyncCompanyStatusTask", rule="*\/15 * * * * *", callback="execute", memo="这是一个示例的定时任务")
 */
class SyncCompanyStatusTask
{

    /**
     * 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象
     *
     * @Inject
     * @var \App\FooRedis
     */
    private $redis;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $actionLogger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
        $this->actionLogger = $loggerFactory->get('log', 'dbAction');
    }


    public function execute()
    {

        $updateDate = '2000-01-01 00:00:00';
        $redisKey = $this->redis->bsKey['TABLE_COMPANY_UPDATE_DATE'];
        $redisCheckKey = $this->redis->bsKey['TABLE_COMPANY_CHECK'];
        if ($this->redis->get($redisCheckKey) == 1)
            return false;

        $this->redis->set($redisCheckKey,1);
        if ($this->redis->exists($redisKey))
            $updateDate = $this->redis->get($redisKey);

//        $this->logger->info("key" .$redisKey);
        $companies = ZaXApiCompany::query()->where("update_date",">",$updateDate)->orderBy("update_date","ASC")->get();
        if (count($companies) > 0){
            foreach ($companies as $company){
                try{
                    $xrCompany = XApiCompany::query()->findOrFail($company->id1);
//                    var_dump($company->update_date);
//                    var_dump($xrCompany->update_date);
                    if ($xrCompany->update_date < $company->update_date){
                        if ($xrCompany->status != $company->status){
                            $oldCompanyStatus = $xrCompany->status;
                            $xrCompany->status = $company->status;
                            $xrCompany->update_date = $company->update_date;
                            $xrCompany->save();
                            $this->actionLogger->info("场所状态被更新 场所id:".$company->id1.",修改前的场所状态".$oldCompanyStatus.",修改后的场所状态".$xrCompany->status);
                        }
                    } //当主库的更新时间小于内网的更新时间，说明内网有修改


                }catch (ModelNotFoundException $exception){
                    $this->logger->info("不存在的companyId" .$company->id1);
                }
                $newUpdateDate = $company->update_date;
            }
            $this->logger->info("设置最新时间" .$newUpdateDate);
            $this->redis->set($redisKey,$newUpdateDate);
        }

        $this->redis->set($redisCheckKey,0);

    }
}